package logic

import (
	"context"
	"goods/goods"
	"goods/internal/model"
	"goods/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *goods.GoodsListRequest) (*goods.GoodsListResponse, error) {
	goodsModel := model.NewGoodsModel(l.ctx, l.svcCtx)

	goodsList, err := goodsModel.GoodsList(in.GoodsName)
	if err != nil {
		return nil, err
	}

	var goodsInfo []*goods.GoodsInfo

	for _, goodsListItem := range goodsList {

		g := &goods.GoodsInfo{
			ID:          int64(goodsListItem.ID),
			GoodsName:   goodsListItem.GoodsName,
			GoodsStock:  int64(goodsListItem.GoodsStock),
			GoodsPrice:  float32(goodsListItem.GoodsPrice),
			GoodsImage:  goodsListItem.GoodsImage,
			GoodsStatus: int64(goodsListItem.GoodsStatus),
		}
		goodsInfo = append(goodsInfo, g)
	}

	return &goods.GoodsListResponse{List: goodsInfo}, nil
}
